﻿using RepositoryUtil.SQL;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XRepair.Models;

namespace MvcXRepairAdmin.Repository.SQL
{
    public class OrderStatusDAO : BaseDAO
    {
        public IEnumerable<OrderStatus> GetAllOrderStatus()
        {
            string spName = "GetAllOrderStatus";

            DataTable resultTab = null;
            List<OrderStatus> orderStatusList = null;

            using (var conn = XRepairConn)
            {
                conn.Open();

                SqlCommand command = new SqlCommand(spName, conn);
                command.CommandType = CommandType.StoredProcedure;
                resultTab = command.ExecuteDataTable();
            }
            if (resultTab != null && resultTab.Rows.Count > 0)
            {
                orderStatusList = this.ConvertDataTableToOrderStatusList(resultTab);
            }

            return orderStatusList;
        }

        private List<OrderStatus> ConvertDataTableToOrderStatusList(DataTable data)
        {
            List<OrderStatus> OrderStatus = null;

            if (data != null)
            {
                OrderStatus = new List<OrderStatus>();

                foreach (DataRow dr in data.Rows)
                {
                    var serviceModel = new OrderStatus();

                    serviceModel.ID = SqlUtil<int>.GetValue(dr, "ID");
                    serviceModel.Description = SqlUtil<string>.GetValue(dr, "Description");

                    OrderStatus.Add(serviceModel);
                }
            }

            return OrderStatus;
        }
    }
}
